﻿using System;
using System.Collections.Generic;
using System.Windows.Media.Imaging;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using WickedFlame.Core.Layers;
using System.IO;
using WickedFlame.Core;
using WickedFlame.Util.Actions;
using WickedFlame.Core.Shapes;

namespace WickedFlame.Util.Data
{
    public class JpegFileFormat : FileFormat
    {
        public override void Export(DrawingCanvas canvas, Stream stream)
        {
            JpegBitmapEncoder encoder = new JpegBitmapEncoder();
            
            // Get the size of canvas
            Size size = new Size(canvas.Width, canvas.Height);

            // Create a render bitmap and push the surface to it
            RenderTargetBitmap renderBitmap =
              new RenderTargetBitmap(
                (int)size.Width,
                (int)size.Height,
                96d,
                96d,
                PixelFormats.Pbgra32);

            //TODO:
            //create white background!
            
            bool transparentBackground = false;
            if (canvas.Background == null || canvas.Background == Brushes.Transparent)
            {
                canvas.Background = Brushes.White;
                transparentBackground = true;
            }

            //foreach (Canvas surface in surfaces)
            //foreach (FrameworkElement surface in surfaces)
            foreach (Layer layer in canvas.Layers)
            {
                //render/push each canvas
                if (layer.IsVisible)
                    renderBitmap.Render(layer);
            }

            encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
            //TODO:
            //Qualitylevel has to be variable!!
            encoder.QualityLevel = 100;
            // save the data to the stream
            encoder.Save(stream);

            if (transparentBackground)
                canvas.Background = Brushes.Transparent;
        }

        public override DrawingCanvas Import(Stream stream)
        {
            JpegBitmapDecoder decoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            stream.Seek(0, System.IO.SeekOrigin.Begin);

            BitmapImage image = new BitmapImage();
            image.BeginInit();
            image.CacheOption = BitmapCacheOption.OnLoad;
            image.StreamSource = stream;
            image.EndInit();

            //convert to image with alphachanel
            ConvertImageToPbgra32Action action = new ConvertImageToPbgra32Action();
            image = action.ConvertBitmapFormat(image);

            var canvas = new DrawingCanvas();
            canvas.Width = image.Width;
            canvas.Height = image.Height;
            canvas.AddLayer();

            var shape = new ImageShape(image);
            canvas.AddShape(shape, true);
            shape.Invalidate();

            return canvas;
        }
    }
}
